package com.base.cn.platform.os.controller.user.integral;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegralRecord;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralRecordService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台积分规则
 *
 * @author ll
 * @create 2018-04-27
 */
@Controller
@RequestMapping("/admin/user/cus")
public class AdminCusUserIntegralRuleController extends AdminBaseController {

    @Autowired
    private CusUserIntegralRuleService cusUserIntegralRuleService;

    @Autowired
    private CusUserIntegralRecordService cusUserIntegralRecordService;

    /**
     * 去列表页面
     * @return
     */
    @RequestMapping("/toCusUserIntegralRuleListPage")
    public String toCusUserIntegralRuleListPage(){
        return resultPage("/user/integral/integral_list");
    }

    /**
     * 搜索经验规则明细列表
     * @param cusUserIntegralRule
     * @return
     */
    @RequestMapping("/queryAjaxUserIntegralRuleList")
    public String queryAjaxUserIntegralRuleList(Model model,@RequestBody Map<String, Object> cusUserIntegralRule ,
                                                @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Map<String,Object> dataMap = cusUserIntegralRuleService.queryAjaxUserIntegralRuleList(cusUserIntegralRule,currentPage);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/integral/integral_ajax_list");
    }

    /**
     * 去添加页面
     * @return
     */
    @RequestMapping("/toSaveCusUserIntegralRule")
    public String toSaveCusUserIntegralRule(){
       return resultPage("/user/integral/integral_create");
    }



    /**
     * 添加方法
     * @param userIntegralRule
     * @return
     */
    @RequestMapping("/saveUserIntegralRule")
    @ResponseBody
    Map<String ,Object> saveUserIntegralRule(@RequestBody Map<String,Object> userIntegralRule){
        return cusUserIntegralRuleService.saveUserIntegralRule(userIntegralRule);
    }

    /**
     * 修改积分
     * @param id
     * @param score
     * @return
     */
    @RequestMapping("/saveUserIntegralRuleScore")
    @ResponseBody
    Map<String ,Object> saveUserIntegralRuleScore(@RequestParam("id") BigDecimal id,
                                             @RequestParam("score")Integer score){
        Map<String,Object> userIntegralRuleMap = new HashMap<>();
        if(!DataUtil.idIsNotNull(id)){
            return ResultUtil.ERROR("修改目标ID不能为空");
        }
        if(score==null||score==0){
            return ResultUtil.ERROR("修改积分不能为0");
        }
        userIntegralRuleMap.put("id",id);
        userIntegralRuleMap.put("score",score);
        return cusUserIntegralRuleService.saveUserIntegralRule(userIntegralRuleMap);
    }



    /**
     * 修改积分规则状态
     * @param integralRuleId
     * @param status
     * @return
     */
    @RequestMapping("/updateUserIntegralRuleStatus")
    @ResponseBody
    Map<String,Object> updateUserIntegralRuleStatus(@RequestParam("integralRuleId")BigDecimal integralRuleId,
                                                    @RequestParam("status") Integer status){
        return cusUserIntegralRuleService.updateUserIntegralRuleStatus(integralRuleId,status);
    }


    /**-----------------------------------积分管理-------------------------------------*/

    /**
     * 积分管理/积分配置页面
     * @param type config 积分配置页  rule   积分规则页
     * @return
     */
    @RequestMapping("/{type}/toIntegralRule")
    public String toIntegralRule(Model model, @PathVariable("type")String type){
        model.addAttribute("type", type);
        if ("rule".equals(type)) {
            model.addAttribute("IntegralStatis", cusUserIntegralRecordService.getIntegralStatis());
        }
        model.addAttribute("integralRuleMap", cusUserIntegralRuleService.integralRuleData(type));
        return resultPage("/user/integral/integralManager");
    }

    /**
     * 添加积分规则key
     *
     * @param integralRule
     * @return
     */
    @RequestMapping("/addIntegralRule")
    @ResponseBody
    public Map<String, Object> addIntegralRule(@RequestBody Map<String,Object> integralRule) {
        return cusUserIntegralRuleService.addIntegralRule(integralRule);
    }


    /**
     * 打开积分规则弹窗更新积分规则页面
     *
     * @param key 积分规则标识
     * @param type  config  积分配置，  rule 积分规则
     * @return
     */
    @RequestMapping("/{type}/toUpdateIntegralRule")
    public String toUpdateIntegralRule(Model model,@PathVariable("type")String type, @RequestParam("key") String key, @RequestParam(value = "isList", defaultValue = "false")Boolean isList) {
        model.addAttribute("isList", isList);
        model.addAttribute("key", key);
        model.addAttribute("type", type);
        model.addAttribute("integralRuleMap", cusUserIntegralRuleService.integralRuleData(type));
        return resultPage("/user/integral/show-integral-rule-info");
    }

    /**
     * 更新积分规则
     *
     * @return
     */
    @RequestMapping("/updateIntegralRuleConfig")
    @ResponseBody
    public Map<String, Object> updateIntegralRuleConfig(@RequestBody Map<String, Object> data) {
        return cusUserIntegralRuleService.updateIntegralRuleConfig(data);
    }

}
